import threading
from abc import ABC, abstractmethod


class AbstractCrawlerHandler(ABC):
    def __init__(self, browser: 'Chromium'):
        self.browser = browser
        self.lock = threading.Lock()  # 创建一个锁实例

    @abstractmethod
    def crawl(self):
        pass


class MyCrawlerHandler(AbstractCrawlerHandler):
    def crawl(self):
        with self.lock:  # 确保这段代码在同一时间只能被一个线程执行
            tab = self.browser.new_tab()
            # 进行其他操作...
            print(f"New tab created: {tab}")


# 假设这是你的Chromium类
class Chromium:
    def new_tab(self):
        # 模拟创建新标签页的行为
        return "tab_{}".format(id(self))


# 示例：创建一个简单的多线程环境测试上述代码
def worker(crawler):
    crawler.crawl()


if __name__ == "__main__":
    chromium = Chromium()
    crawler_handler = MyCrawlerHandler(chromium)

    thread1 = threading.Thread(target=worker, args=(crawler_handler,))
    thread2 = threading.Thread(target=worker, args=(crawler_handler,))

    thread1.start()
    thread2.start()

    thread1.join()
    thread2.join()